
<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>pymatgen.util.testing &#8212; pymatgen 2020.7.3 documentation</title>
    <link rel="stylesheet" href="../../../_static/basic.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <script id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
    <script src="../../../_static/jquery.js"></script>
    <script src="../../../_static/underscore.js"></script>
    <script src="../../../_static/doctools.js"></script>
    <script src="../../../_static/language_data.js"></script>
    <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" />
 
<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-33990148-1']);
  _gaq.push(['_trackPageview']);
</script>

  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../index.html">pymatgen 2020.7.3 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="../../pymatgen.html" accesskey="U">pymatgen</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">pymatgen.util.testing</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
          <div class="body" role="main">
            
  <h1>Source code for pymatgen.util.testing</h1><div class="highlight"><pre>
<span></span><span class="c1"># coding: utf-8</span>
<span class="c1"># Copyright (c) Pymatgen Development Team.</span>
<span class="c1"># Distributed under the terms of the MIT License.</span>

<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">Common test support for pymatgen test scripts.</span>

<span class="sd">This single module should provide all the common functionality for pymatgen</span>
<span class="sd">tests in a single location, so that test scripts can just import it and work</span>
<span class="sd">right away.</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="kn">import</span> <span class="nn">unittest</span>
<span class="kn">import</span> <span class="nn">tempfile</span>
<span class="kn">import</span> <span class="nn">numpy.testing</span> <span class="k">as</span> <span class="nn">nptu</span>
<span class="kn">from</span> <span class="nn">io</span> <span class="kn">import</span> <span class="nb">open</span>
<span class="kn">from</span> <span class="nn">pathlib</span> <span class="kn">import</span> <span class="n">Path</span>
<span class="kn">import</span> <span class="nn">json</span>

<span class="kn">from</span> <span class="nn">monty.json</span> <span class="kn">import</span> <span class="n">MontyDecoder</span>
<span class="kn">from</span> <span class="nn">monty.serialization</span> <span class="kn">import</span> <span class="n">loadfn</span>
<span class="kn">from</span> <span class="nn">monty.json</span> <span class="kn">import</span> <span class="n">MSONable</span>
<span class="kn">from</span> <span class="nn">monty.dev</span> <span class="kn">import</span> <span class="n">requires</span>

<span class="kn">from</span> <span class="nn">pymatgen</span> <span class="kn">import</span> <span class="n">SETTINGS</span><span class="p">,</span> <span class="n">MPRester</span>


<div class="viewcode-block" id="PymatgenTest"><a class="viewcode-back" href="../../../pymatgen.util.testing.html#pymatgen.util.testing.PymatgenTest">[docs]</a><span class="k">class</span> <span class="nc">PymatgenTest</span><span class="p">(</span><span class="n">unittest</span><span class="o">.</span><span class="n">TestCase</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Extends unittest.TestCase with functions (taken from numpy.testing.utils)</span>
<span class="sd">    that support the comparison of arrays.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">_multiprocess_shared_</span> <span class="o">=</span> <span class="kc">True</span>
    <span class="n">MODULE_DIR</span> <span class="o">=</span> <span class="n">Path</span><span class="p">(</span><span class="vm">__file__</span><span class="p">)</span><span class="o">.</span><span class="n">absolute</span><span class="p">()</span><span class="o">.</span><span class="n">parent</span>
    <span class="n">STRUCTURES_DIR</span> <span class="o">=</span> <span class="n">MODULE_DIR</span> <span class="o">/</span> <span class="s2">&quot;structures&quot;</span>
    <span class="n">TEST_FILES_DIR</span> <span class="o">=</span> <span class="n">MODULE_DIR</span> <span class="o">/</span> <span class="s2">&quot;..&quot;</span> <span class="o">/</span> <span class="s2">&quot;..&quot;</span> <span class="o">/</span> <span class="s2">&quot;test_files&quot;</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Dict for test structures to aid testing.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">TEST_STRUCTURES</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="k">for</span> <span class="n">fn</span> <span class="ow">in</span> <span class="n">STRUCTURES_DIR</span><span class="o">.</span><span class="n">iterdir</span><span class="p">():</span>
        <span class="n">TEST_STRUCTURES</span><span class="p">[</span><span class="n">fn</span><span class="o">.</span><span class="n">name</span><span class="o">.</span><span class="n">rsplit</span><span class="p">(</span><span class="s2">&quot;.&quot;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)[</span><span class="mi">0</span><span class="p">]]</span> <span class="o">=</span> <span class="n">loadfn</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">fn</span><span class="p">))</span>

<div class="viewcode-block" id="PymatgenTest.get_structure"><a class="viewcode-back" href="../../../pymatgen.util.testing.html#pymatgen.util.testing.PymatgenTest.get_structure">[docs]</a>    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">get_structure</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get a structure from the template directories.</span>

<span class="sd">        :param name: Name of a structure.</span>
<span class="sd">        :return: Structure</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">cls</span><span class="o">.</span><span class="n">TEST_STRUCTURES</span><span class="p">[</span><span class="n">name</span><span class="p">]</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span></div>

<div class="viewcode-block" id="PymatgenTest.get_mp_structure"><a class="viewcode-back" href="../../../pymatgen.util.testing.html#pymatgen.util.testing.PymatgenTest.get_mp_structure">[docs]</a>    <span class="nd">@classmethod</span>
    <span class="nd">@requires</span><span class="p">(</span><span class="n">SETTINGS</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;PMG_MAPI_KEY&quot;</span><span class="p">),</span> <span class="s2">&quot;PMG_MAPI_KEY needs to be set.&quot;</span><span class="p">)</span>
    <span class="k">def</span> <span class="nf">get_mp_structure</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">mpid</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get a structure from MP.</span>

<span class="sd">        :param mpid: Materials Project id.</span>
<span class="sd">        :return: Structure</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">m</span> <span class="o">=</span> <span class="n">MPRester</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">m</span><span class="o">.</span><span class="n">get_structure_by_material_id</span><span class="p">(</span><span class="n">mpid</span><span class="p">)</span></div>

<div class="viewcode-block" id="PymatgenTest.assertArrayAlmostEqual"><a class="viewcode-back" href="../../../pymatgen.util.testing.html#pymatgen.util.testing.PymatgenTest.assertArrayAlmostEqual">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">assertArrayAlmostEqual</span><span class="p">(</span><span class="n">actual</span><span class="p">,</span> <span class="n">desired</span><span class="p">,</span> <span class="n">decimal</span><span class="o">=</span><span class="mi">7</span><span class="p">,</span> <span class="n">err_msg</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">,</span>
                               <span class="n">verbose</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Tests if two arrays are almost equal to a tolerance. The CamelCase</span>
<span class="sd">        naming is so that it is consistent with standard unittest methods.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">nptu</span><span class="o">.</span><span class="n">assert_almost_equal</span><span class="p">(</span><span class="n">actual</span><span class="p">,</span> <span class="n">desired</span><span class="p">,</span> <span class="n">decimal</span><span class="p">,</span> <span class="n">err_msg</span><span class="p">,</span>
                                        <span class="n">verbose</span><span class="p">)</span></div>

<div class="viewcode-block" id="PymatgenTest.assertDictsAlmostEqual"><a class="viewcode-back" href="../../../pymatgen.util.testing.html#pymatgen.util.testing.PymatgenTest.assertDictsAlmostEqual">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">assertDictsAlmostEqual</span><span class="p">(</span><span class="n">actual</span><span class="p">,</span> <span class="n">desired</span><span class="p">,</span> <span class="n">decimal</span><span class="o">=</span><span class="mi">7</span><span class="p">,</span> <span class="n">err_msg</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">,</span>
                               <span class="n">verbose</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Tests if two arrays are almost equal to a tolerance. The CamelCase</span>
<span class="sd">        naming is so that it is consistent with standard unittest methods.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">actual</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="k">if</span> <span class="n">k</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">desired</span><span class="p">:</span>
                <span class="k">return</span> <span class="kc">False</span>
            <span class="n">v2</span> <span class="o">=</span> <span class="n">desired</span><span class="p">[</span><span class="n">k</span><span class="p">]</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">v</span><span class="p">,</span> <span class="nb">dict</span><span class="p">):</span>
                <span class="n">pass_test</span> <span class="o">=</span> <span class="n">PymatgenTest</span><span class="o">.</span><span class="n">assertDictArraysAlmostEqual</span><span class="p">(</span>
                    <span class="n">v</span><span class="p">,</span> <span class="n">v2</span><span class="p">,</span> <span class="n">decimal</span><span class="o">=</span><span class="n">decimal</span><span class="p">,</span> <span class="n">err_msg</span><span class="o">=</span><span class="n">err_msg</span><span class="p">,</span> <span class="n">verbose</span><span class="o">=</span><span class="n">verbose</span><span class="p">)</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">pass_test</span><span class="p">:</span>
                    <span class="k">return</span> <span class="kc">False</span>
            <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">v</span><span class="p">,</span> <span class="p">(</span><span class="nb">list</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">)):</span>
                <span class="n">pass_test</span> <span class="o">=</span> <span class="n">nptu</span><span class="o">.</span><span class="n">assert_almost_equal</span><span class="p">(</span><span class="n">v</span><span class="p">,</span> <span class="n">v2</span><span class="p">,</span> <span class="n">decimal</span><span class="p">,</span> <span class="n">err_msg</span><span class="p">,</span>
                                                     <span class="n">verbose</span><span class="p">)</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">pass_test</span><span class="p">:</span>
                    <span class="k">return</span> <span class="kc">False</span>
            <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">v</span><span class="p">,</span> <span class="p">(</span><span class="nb">int</span><span class="p">,</span> <span class="nb">float</span><span class="p">)):</span>
                <span class="n">pass_test</span> <span class="o">=</span> <span class="n">PymatgenTest</span><span class="o">.</span><span class="n">assertAlmostEqual</span><span class="p">(</span><span class="n">v</span><span class="p">,</span> <span class="n">v2</span><span class="p">)</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">pass_test</span><span class="p">:</span>
                    <span class="k">return</span> <span class="kc">False</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">assert</span> <span class="n">v</span> <span class="o">==</span> <span class="n">v2</span>
        <span class="k">return</span> <span class="kc">True</span></div>

<div class="viewcode-block" id="PymatgenTest.assertArrayEqual"><a class="viewcode-back" href="../../../pymatgen.util.testing.html#pymatgen.util.testing.PymatgenTest.assertArrayEqual">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">assertArrayEqual</span><span class="p">(</span><span class="n">actual</span><span class="p">,</span> <span class="n">desired</span><span class="p">,</span> <span class="n">err_msg</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">verbose</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Tests if two arrays are equal. The CamelCase naming is so that it is</span>
<span class="sd">         consistent with standard unittest methods.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">nptu</span><span class="o">.</span><span class="n">assert_equal</span><span class="p">(</span><span class="n">actual</span><span class="p">,</span> <span class="n">desired</span><span class="p">,</span> <span class="n">err_msg</span><span class="o">=</span><span class="n">err_msg</span><span class="p">,</span>
                                 <span class="n">verbose</span><span class="o">=</span><span class="n">verbose</span><span class="p">)</span></div>

<div class="viewcode-block" id="PymatgenTest.assertStrContentEqual"><a class="viewcode-back" href="../../../pymatgen.util.testing.html#pymatgen.util.testing.PymatgenTest.assertStrContentEqual">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">assertStrContentEqual</span><span class="p">(</span><span class="n">actual</span><span class="p">,</span> <span class="n">desired</span><span class="p">,</span> <span class="n">err_msg</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">verbose</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Tests if two strings are equal, ignoring things like trailing spaces,</span>
<span class="sd">        etc.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">lines1</span> <span class="o">=</span> <span class="n">actual</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
        <span class="n">lines2</span> <span class="o">=</span> <span class="n">desired</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">lines1</span><span class="p">)</span> <span class="o">!=</span> <span class="nb">len</span><span class="p">(</span><span class="n">lines2</span><span class="p">):</span>
            <span class="k">return</span> <span class="kc">False</span>
        <span class="n">failed</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">l1</span><span class="p">,</span> <span class="n">l2</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">lines1</span><span class="p">,</span> <span class="n">lines2</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">l1</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span> <span class="o">!=</span> <span class="n">l2</span><span class="o">.</span><span class="n">strip</span><span class="p">():</span>
                <span class="n">failed</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">%s</span><span class="s2"> != </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">l1</span><span class="p">,</span> <span class="n">l2</span><span class="p">))</span>
        <span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="n">failed</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span></div>

<div class="viewcode-block" id="PymatgenTest.serialize_with_pickle"><a class="viewcode-back" href="../../../pymatgen.util.testing.html#pymatgen.util.testing.PymatgenTest.serialize_with_pickle">[docs]</a>    <span class="k">def</span> <span class="nf">serialize_with_pickle</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">objects</span><span class="p">,</span> <span class="n">protocols</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">test_eq</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Test whether the object(s) can be serialized and deserialized with</span>
<span class="sd">        pickle. This method tries to serialize the objects with pickle and the</span>
<span class="sd">        protocols specified in input. Then it deserializes the pickle format</span>
<span class="sd">        and compares the two objects with the __eq__ operator if</span>
<span class="sd">        test_eq == True.</span>

<span class="sd">        Args:</span>
<span class="sd">            objects: Object or list of objects.</span>
<span class="sd">            protocols: List of pickle protocols to test. If protocols is None,</span>
<span class="sd">                HIGHEST_PROTOCOL is tested.</span>

<span class="sd">        Returns:</span>
<span class="sd">            Nested list with the objects deserialized with the specified</span>
<span class="sd">            protocols.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># Use the python version so that we get the traceback in case of errors</span>
        <span class="kn">import</span> <span class="nn">pickle</span> <span class="k">as</span> <span class="nn">pickle</span>
        <span class="kn">from</span> <span class="nn">pymatgen.util.serialization</span> <span class="kn">import</span> <span class="n">pmg_pickle_load</span><span class="p">,</span> \
            <span class="n">pmg_pickle_dump</span>

        <span class="c1"># Build a list even when we receive a single object.</span>
        <span class="n">got_single_object</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">objects</span><span class="p">,</span> <span class="p">(</span><span class="nb">list</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">)):</span>
            <span class="n">got_single_object</span> <span class="o">=</span> <span class="kc">True</span>
            <span class="n">objects</span> <span class="o">=</span> <span class="p">[</span><span class="n">objects</span><span class="p">]</span>

        <span class="k">if</span> <span class="n">protocols</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="c1"># protocols = set([0, 1, 2] + [pickle.HIGHEST_PROTOCOL])</span>
            <span class="n">protocols</span> <span class="o">=</span> <span class="p">[</span><span class="n">pickle</span><span class="o">.</span><span class="n">HIGHEST_PROTOCOL</span><span class="p">]</span>

        <span class="c1"># This list will contains the object deserialized with the different</span>
        <span class="c1"># protocols.</span>
        <span class="n">objects_by_protocol</span><span class="p">,</span> <span class="n">errors</span> <span class="o">=</span> <span class="p">[],</span> <span class="p">[]</span>

        <span class="k">for</span> <span class="n">protocol</span> <span class="ow">in</span> <span class="n">protocols</span><span class="p">:</span>
            <span class="c1"># Serialize and deserialize the object.</span>
            <span class="n">mode</span> <span class="o">=</span> <span class="s2">&quot;wb&quot;</span>
            <span class="n">fd</span><span class="p">,</span> <span class="n">tmpfile</span> <span class="o">=</span> <span class="n">tempfile</span><span class="o">.</span><span class="n">mkstemp</span><span class="p">(</span><span class="n">text</span><span class="o">=</span><span class="s2">&quot;b&quot;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">mode</span><span class="p">)</span>

            <span class="k">try</span><span class="p">:</span>
                <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">tmpfile</span><span class="p">,</span> <span class="n">mode</span><span class="p">)</span> <span class="k">as</span> <span class="n">fh</span><span class="p">:</span>
                    <span class="n">pmg_pickle_dump</span><span class="p">(</span><span class="n">objects</span><span class="p">,</span> <span class="n">fh</span><span class="p">,</span> <span class="n">protocol</span><span class="o">=</span><span class="n">protocol</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">exc</span><span class="p">:</span>
                <span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot;pickle.dump with protocol </span><span class="si">%s</span><span class="s2"> raised:</span><span class="se">\n</span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span>
                              <span class="p">(</span><span class="n">protocol</span><span class="p">,</span> <span class="nb">str</span><span class="p">(</span><span class="n">exc</span><span class="p">)))</span>
                <span class="k">continue</span>

            <span class="k">try</span><span class="p">:</span>
                <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">tmpfile</span><span class="p">,</span> <span class="s2">&quot;rb&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">fh</span><span class="p">:</span>
                    <span class="n">new_objects</span> <span class="o">=</span> <span class="n">pmg_pickle_load</span><span class="p">(</span><span class="n">fh</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">exc</span><span class="p">:</span>
                <span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot;pickle.load with protocol </span><span class="si">%s</span><span class="s2"> raised:</span><span class="se">\n</span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span>
                              <span class="p">(</span><span class="n">protocol</span><span class="p">,</span> <span class="nb">str</span><span class="p">(</span><span class="n">exc</span><span class="p">)))</span>
                <span class="k">continue</span>

            <span class="c1"># Test for equality</span>
            <span class="k">if</span> <span class="n">test_eq</span><span class="p">:</span>
                <span class="k">for</span> <span class="n">old_obj</span><span class="p">,</span> <span class="n">new_obj</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">objects</span><span class="p">,</span> <span class="n">new_objects</span><span class="p">):</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span><span class="n">old_obj</span><span class="p">,</span> <span class="n">new_obj</span><span class="p">)</span>

            <span class="c1"># Save the deserialized objects and test for equality.</span>
            <span class="n">objects_by_protocol</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">new_objects</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">errors</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">errors</span><span class="p">))</span>

        <span class="c1"># Return nested list so that client code can perform additional tests.</span>
        <span class="k">if</span> <span class="n">got_single_object</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[</span><span class="n">o</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">for</span> <span class="n">o</span> <span class="ow">in</span> <span class="n">objects_by_protocol</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">objects_by_protocol</span></div>

<div class="viewcode-block" id="PymatgenTest.tmpfile_write"><a class="viewcode-back" href="../../../pymatgen.util.testing.html#pymatgen.util.testing.PymatgenTest.tmpfile_write">[docs]</a>    <span class="k">def</span> <span class="nf">tmpfile_write</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">string</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Write string to a temporary file. Returns the name of the temporary</span>
<span class="sd">        file.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">fd</span><span class="p">,</span> <span class="n">tmpfile</span> <span class="o">=</span> <span class="n">tempfile</span><span class="o">.</span><span class="n">mkstemp</span><span class="p">(</span><span class="n">text</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>

        <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">tmpfile</span><span class="p">,</span> <span class="s2">&quot;w&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">fh</span><span class="p">:</span>
            <span class="n">fh</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">string</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">tmpfile</span></div>

<div class="viewcode-block" id="PymatgenTest.assertMSONable"><a class="viewcode-back" href="../../../pymatgen.util.testing.html#pymatgen.util.testing.PymatgenTest.assertMSONable">[docs]</a>    <span class="k">def</span> <span class="nf">assertMSONable</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj</span><span class="p">,</span> <span class="n">test_if_subclass</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Tests if obj is MSONable and tries to verify whether the contract is</span>
<span class="sd">        fulfilled.</span>

<span class="sd">        By default, the method tests whether obj is an instance of MSONable.</span>
<span class="sd">        This check can be deactivated by setting test_if_subclass to False.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">test_if_subclass</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">assertIsInstance</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">MSONable</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">assertDictEqual</span><span class="p">(</span><span class="n">obj</span><span class="o">.</span><span class="n">as_dict</span><span class="p">(),</span> <span class="n">obj</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="n">from_dict</span><span class="p">(</span>
            <span class="n">obj</span><span class="o">.</span><span class="n">as_dict</span><span class="p">())</span><span class="o">.</span><span class="n">as_dict</span><span class="p">())</span>
        <span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">obj</span><span class="o">.</span><span class="n">to_json</span><span class="p">(),</span> <span class="bp">cls</span><span class="o">=</span><span class="n">MontyDecoder</span><span class="p">)</span></div></div>
</pre></div>

            <div class="clearer"></div>
          </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../index.html">pymatgen 2020.7.3 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="../../pymatgen.html" >pymatgen</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">pymatgen.util.testing</a></li> 
      </ul>
    </div>

    <div class="footer" role="contentinfo">
        &#169; Copyright 2011, Pymatgen Development Team.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 3.1.2.
    </div>
<div class="footer">This page uses <a href="http://analytics.google.com/">
Google Analytics</a> to collect statistics. You can disable it by blocking
the JavaScript coming from www.google-analytics.com.
<script type="text/javascript">
  (function() {
    var ga = document.createElement('script');
    ga.src = ('https:' == document.location.protocol ?
              'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    ga.setAttribute('async', 'true');
    document.documentElement.firstChild.appendChild(ga);
  })();
</script>
</div>

  </body>
</html>